package com.example.smartgreenhouse.WebSocketServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class AirDataWebSocketServer {
    private final SimpMessagingTemplate messagingTemplate;

    @Autowired
    public AirDataWebSocketServer(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendAirData(int temperature,int temperatureChangeValue,float temperatureChangeRate,
                            int humidity,int humidityChangeValue,float humidityChangeRate,
                            LocalDateTime collectionTime){
        Map<String, Object> message = new HashMap<>();
        message.put("air_temperature", temperature);
        message.put("temperature_change_value", temperatureChangeValue);
        message.put("temperature_change_rate", temperatureChangeRate);
        message.put("air_humidity", humidity);
        message.put("humidity_change_value", humidityChangeValue);
        message.put("humidity_change_rate", humidityChangeRate);
        message.put("collection_time", collectionTime);
        messagingTemplate.convertAndSend("/topic/air_data", message);
    }
}
